package com.ssd.job;

import com.ssd.utils.StringUtils;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * XXL-JOB配置类
 * 该类用于初始化和配置XXL-JOB的执行器
 */
@Slf4j
@Configuration
@ConditionalOnProperty(prefix = "xxl-job", value = "enable", havingValue = "true")
//@EnableConfigurationProperties(XxlJobProperties.class)
public class XxlJobConfiguration {

    /**
     * 创建XXL-JOB执行器实例
     * 该方法根据配置文件中的属性初始化XXL-JOB的执行器，并设置相关属性
     *
     * @param prop XXL-JOB的配置属性，包含管理员和执行器的配置
     * @return 返回配置好的XXL-JOB执行器实例
     */
    @Bean
    public XxlJobSpringExecutor xxlJobExecutor(XxlJobProperties prop) {
        log.info(">>>>>>>>>>> xxl-job config init.");
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        // 设置管理员地址
        XxlJobProperties.Admin admin = prop.getAdmin();
        if (admin != null && StringUtils.isNotEmpty(admin.getAddress())) {
            xxlJobSpringExecutor.setAdminAddresses(admin.getAddress());
        }
        // 设置访问令牌
//        xxlJobSpringExecutor.setAccessToken(prop.getAccessToken());
        // 设置执行器属性
        XxlJobProperties.Executor executor = prop.getExecutor();
        if (executor != null) {
            xxlJobSpringExecutor.setIp(executor.getIp());
            xxlJobSpringExecutor.setAppname(executor.getAppName());
            xxlJobSpringExecutor.setPort(executor.getPort());
            xxlJobSpringExecutor.setLogPath(executor.getLogPath());
            xxlJobSpringExecutor.setLogRetentionDays(
                    executor.getLogRetentionDays()
            );
        }

        log.info(">>>>>>>>>>> xxl-job config end.");
        return xxlJobSpringExecutor;
    }
}

